home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 October / CHIP Turkiye Ekim 2000.iso / prog / naps / 04 / setup.exe / Gnucleus / InPlaceEdit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-06  |  4.3 KB  |  181 lines

  1. /********************************************************************************
  2.  
  3.     Gnucleus - A node application for the Gnutella network
  4.     Copyright (C) 2000 John Marshall
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.     For support, questions, comments, etc...
  20.     E-Mail: 
  21.         swabby@c0re.net
  22.     
  23.     Address:
  24.         21 Cadogan Way
  25.         Nashua, NH, USA 03062
  26.  
  27. ********************************************************************************/
  28.  
  29. #include "stdafx.h"
  30. #include "InPlaceEdit.h"
  31.  
  32. #ifdef _DEBUG
  33. #define new DEBUG_NEW
  34. #undef THIS_FILE
  35. static char THIS_FILE[] = __FILE__;
  36. #endif
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CInPlaceEdit
  40.  
  41. CInPlaceEdit::CInPlaceEdit(int iItem, int iSubItem, CString sInitText)
  42. :m_sInitText( sInitText )
  43. {
  44.     m_iItem = iItem;
  45.     m_iSubItem = iSubItem;
  46.     m_bESC = FALSE;
  47. }
  48.  
  49. CInPlaceEdit::~CInPlaceEdit()
  50. {
  51. }
  52.  
  53.  
  54. BEGIN_MESSAGE_MAP(CInPlaceEdit, CEdit)
  55.     //{{AFX_MSG_MAP(CInPlaceEdit)
  56.     ON_WM_KILLFOCUS()
  57.     ON_WM_NCDESTROY()
  58.     ON_WM_CHAR()
  59.     ON_WM_CREATE()
  60.     //}}AFX_MSG_MAP
  61. END_MESSAGE_MAP()
  62.  
  63. /////////////////////////////////////////////////////////////////////////////
  64. // CInPlaceEdit message handlers
  65.  
  66. BOOL CInPlaceEdit::PreTranslateMessage(MSG* pMsg)
  67. {
  68.     if( pMsg->message == WM_KEYDOWN )
  69.     {
  70.         if(pMsg->wParam == VK_RETURN
  71.                 || pMsg->wParam == VK_DELETE
  72.                 || pMsg->wParam == VK_ESCAPE
  73.                 || GetKeyState( VK_CONTROL)
  74.                 )
  75.         {
  76.             ::TranslateMessage(pMsg);
  77.             ::DispatchMessage(pMsg);
  78.             return TRUE;                // DO NOT process further
  79.         }
  80.     }
  81.  
  82.     return CEdit::PreTranslateMessage(pMsg);
  83. }
  84.  
  85.  
  86. void CInPlaceEdit::OnKillFocus(CWnd* pNewWnd)
  87. {
  88.     CEdit::OnKillFocus(pNewWnd);
  89.  
  90.     CString str;
  91.     GetWindowText(str);
  92.  
  93.     // Send Notification to parent of ListView ctrl
  94.     LV_DISPINFO dispinfo;
  95.     dispinfo.hdr.hwndFrom = GetParent()->m_hWnd;
  96.     dispinfo.hdr.idFrom = GetDlgCtrlID();
  97.     dispinfo.hdr.code = LVN_ENDLABELEDIT;
  98.  
  99.     dispinfo.item.mask = LVIF_TEXT;
  100.     dispinfo.item.iItem = m_iItem;
  101.     dispinfo.item.iSubItem = m_iSubItem;
  102.     dispinfo.item.pszText = m_bESC ? NULL : LPTSTR((LPCTSTR)str);
  103.     dispinfo.item.cchTextMax = str.GetLength();
  104.  
  105.     GetParent()->GetParent()->SendMessage( WM_NOTIFY, GetParent()->GetDlgCtrlID(), 
  106.                     (LPARAM)&dispinfo );
  107.  
  108.     DestroyWindow();
  109. }
  110.  
  111. void CInPlaceEdit::OnNcDestroy()
  112. {
  113.     CEdit::OnNcDestroy();
  114.  
  115.     delete this;
  116. }
  117.  
  118.  
  119. void CInPlaceEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  120. {
  121.     if( nChar == VK_ESCAPE || nChar == VK_RETURN)
  122.     {
  123.         if( nChar == VK_ESCAPE )
  124.             m_bESC = TRUE;
  125.         GetParent()->SetFocus();
  126.         return;
  127.     }
  128.  
  129.  
  130.     CEdit::OnChar(nChar, nRepCnt, nFlags);
  131.  
  132.     // Resize edit control if needed
  133.  
  134.     // Get text extent
  135.     CString str;
  136.  
  137.     GetWindowText( str );
  138.     CWindowDC dc(this);
  139.     CFont *pFont = GetParent()->GetFont();
  140.     CFont *pFontDC = dc.SelectObject( pFont );
  141.     CSize size = dc.GetTextExtent( str );
  142.     dc.SelectObject( pFontDC );
  143.     size.cx += 5;                   // add some extra buffer
  144.  
  145.     // Get client rect
  146.     CRect rect, parentrect;
  147.     GetClientRect( &rect );
  148.     GetParent()->GetClientRect( &parentrect );
  149.  
  150.     // Transform rect to parent coordinates
  151.     ClientToScreen( &rect );
  152.     GetParent()->ScreenToClient( &rect );
  153.  
  154.     // Check whether control needs to be resized
  155.     // and whether there is space to grow
  156.     if( size.cx > rect.Width() )
  157.     {
  158.         if( size.cx + rect.left < parentrect.right )
  159.             rect.right = rect.left + size.cx;
  160.         else
  161.             rect.right = parentrect.right;
  162.         MoveWindow( &rect );
  163.     }
  164. }
  165.  
  166. int CInPlaceEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
  167. {
  168.     if (CEdit::OnCreate(lpCreateStruct) == -1)
  169.         return -1;
  170.  
  171.     // Set the proper font
  172.     CFont* font = GetParent()->GetFont();
  173.     SetFont(font);
  174.  
  175.     SetWindowText( m_sInitText );
  176.     SetFocus();
  177.     SetSel( 0, -1 );
  178.     return 0;
  179. }
  180.  
  181.